home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / LABEL_TO.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  11.0 KB  |  367 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.input.*;
  4. import sub_arctic.output.*;
  5. import sub_arctic.constraints.constraint;
  6.  
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Color;
  10.  
  11. /**
  12.  * These are toggles which occupy a fixed width and have a 
  13.  * label just to the right of the image.  They support the style
  14.  * interface and can be used to create either labeled check boxes
  15.  * or labeled radio_buttons.
  16.  *
  17.  * @author Ian Smith
  18.  */
  19. public class label_toggle extends toggle {
  20.  
  21.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  22.  
  23.   /** Separation between toggle and label. */
  24.   protected static int _default_separation=3;
  25.  
  26.   /**
  27.    * Return the separation between label and toggle.
  28.    * @return int the number of pixels between label and toggle
  29.    */
  30.   public int default_separation() { return _default_separation;};
  31.  
  32.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  33.  
  34.   /**
  35.    * The font for this object. If you don't supply a font, the one chosen
  36.    * for you is the one returned from the style_manager.
  37.    */
  38.   protected Font _font;
  39.  
  40.   /**
  41.    * Return the font for this object. It defaults to being the 
  42.    * one set in the style_manager.
  43.    *
  44.    * @return Font the font being used for this object.
  45.    */
  46.   public Font font() {
  47.     if (_font!=null) {
  48.       return _font;
  49.     } else {
  50.       return style_manager.default_font();
  51.     }
  52.   }
  53.    
  54.   /**
  55.    * Set the font in use for this toggle. If you pass null, you'll get
  56.    * the default font from the style_manager.
  57.    *
  58.    * @param Font f the new font
  59.    */
  60.   public void set_font(Font f) { 
  61.     _font=f;
  62.     compute_images();
  63.   }
  64.  
  65.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  66.  
  67.   /**
  68.    * The text of the label.
  69.    */
  70.   protected String _text;
  71.  
  72.   /**
  73.    * Access the text of the label.
  74.    * @return String the text of the label
  75.    */
  76.   public String text() { return _text;};
  77.  
  78.   /**
  79.    * Modify the string in use by the label
  80.    *  @param String s the string to change the label to.
  81.    */
  82.   public void set_text(String s) { 
  83.     _text=s;
  84.     compute_images();
  85.   };
  86.  
  87.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  88.  
  89.   /**
  90.    * Requested width is the total width of the interactor or -1
  91.    * if we don't care and want to size by string.
  92.    */
  93.   protected int _requested_width;
  94.  
  95.   /**
  96.    * Return the width the creator of the label requested.
  97.    * @return int the size the programmer wanted for this label
  98.    */
  99.   public int requested_width() { return _requested_width;};
  100.  
  101.   /**
  102.    * Set the width to use for labels. 
  103.    * @param int w the new width of the label
  104.    */
  105.   public void set_requested_width(int w) {
  106.     _requested_width=w;
  107.     compute_images();
  108.   }
  109.  
  110.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  111.  
  112.   /**
  113.    * If this is true, we are going to use the image of 
  114.    * radio_button otherwise we use the image of a checkbox.
  115.    */
  116.   protected boolean is_radio_button=true;
  117.  
  118.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  119.  
  120.   /**
  121.    * This builds the images of the text & toggle. 
  122.    * It computes the height from the font metric.
  123.    */
  124.   protected void compute_images() {
  125.     FontMetrics metric=manager.get_metrics(font());
  126.     int font_height = metric.getHeight() - metric.getLeading();
  127.     int font_ascent = metric.getAscent();
  128.     // we assume that all states of the toggle are the same size
  129.     loaded_image[] result, icon;
  130.     int image_height=font_height,image_width,y;
  131.     drawable d;
  132.     
  133.     /* figure out which set of images we should be using */
  134.     icon = images();
  135.  
  136.     /* how wide should it be? */
  137.     if (requested_width()==-1) {
  138.       /* size by string */
  139.       image_width=metric.stringWidth(text())+icon[0].width()+
  140.     default_separation();
  141.     } else {
  142.       /* user told us to use a value */
  143.       image_width=requested_width();
  144.     }
  145.  
  146.     /* is the font smaller than the appearance of the icon? */
  147.     if (image_height<icon[0].height()) {
  148.       image_height=icon[0].height();
  149.     }
  150.  
  151.     /* build the image */
  152.     result=new loaded_image[2];
  153.     if ((image_width==0) || (image_height==0)) {
  154.       System.out.println("Warning: Detected a zero width and/or height " +
  155.              "label toggle. ");
  156.     }
  157.     result[0]=new loaded_image(image_width, image_height);
  158.     result[1]=new loaded_image(image_width, image_height);
  159.  
  160.     /* draw the icon on #0 */
  161.     d=result[0].get_drawable();
  162.  
  163.     /* center it in y*/
  164.     y=(image_height-icon[0].height())/2;
  165.     d.drawImage(icon[0],0,y);
  166.  
  167.     /* set the font and color */
  168.     d.setColor(Color.black);
  169.     d.setFont(font());
  170.  
  171.     /* put in the separation and draw the text */
  172.     d.drawString(text(),icon[0].width()+default_separation(),
  173.          ((image_height-font_height)/2)+font_ascent);
  174.  
  175.     /* draw the icon on #1 */
  176.     d=result[1].get_drawable();
  177.  
  178.     /* center it in y*/
  179.     y=(image_height-icon[1].height())/2;
  180.     d.drawImage(icon[1],0,y);
  181.  
  182.     /* set the font and color */
  183.     d.setFont(font());
  184.     d.setColor(Color.black);
  185.  
  186.     /* put in the separation and draw the text */
  187.     d.drawString(text(),icon[1].width()+default_separation(),
  188.          ((image_height-font_height)/2)+font_ascent);
  189.  
  190.     /* make the state look in the superclass */
  191.     set_looks(result,null);
  192.   }
  193.  
  194.   /* holds the images for this label toggle */
  195.   protected loaded_image[] _images;
  196.  
  197.   /* returns the right set of images */
  198.   public loaded_image[] images() {
  199.     if (_images == null) {
  200.       /* use the style defaults */
  201.       if (is_radio_button) {
  202.     return (style_manager.current_style().radio_button_make_images());
  203.       }else {
  204.     /* we're a toggle */
  205.     return (style_manager.current_style().checkbox_make_images());
  206.       }
  207.     } else {
  208.       /* use the ones we were given */
  209.       return _images;
  210.     }
  211.   }
  212.  
  213.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  214.  
  215.   /**
  216.    * Create a new label_toggle.  Full constructor.
  217.    * 
  218.    * @param int             x        the x position of the object.
  219.    * @param int             y        the y position of the object.
  220.    * @param boolean         radio    true if you want the look of a radio 
  221.    *                                 button, false if you want the look of a 
  222.    *                                 checkbox.
  223.    * @param callback_object call_obj the object to deliver callbacks to.
  224.    * @param int             width    the width of the label in pixels.
  225.    * @param Font            f        the font to use for this object (or pass 
  226.    *                                 null to get the default font).
  227.    * @param String          l        the label's string.
  228.    */
  229.   public label_toggle(int x, int y, boolean radio,
  230.               callback_object call_obj, int width, 
  231.               Font f, String l) 
  232. {
  233.  
  234.     /* do the super initialization */
  235.     super(x,y,call_obj);
  236.  
  237.     _requested_width=width;
  238.  
  239.     /* set which kind we are */
  240.     is_radio_button=radio;
  241.  
  242.     /* set the font */
  243.       _font=f;
  244.  
  245.     /* now fix the text and draw the images */
  246.     set_text(l);
  247.   }
  248.  
  249.    //had:
  250.    //* @exception bad_value PROPAGATED
  251.    //* @exception general PROPAGATED
  252.  
  253.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  254.  
  255.   /**
  256.    * Create a new label_toggle. You don't need to supply the coordinates
  257.    * because we assume you are using constraints to locate the object
  258.    * on the screen.
  259.    * 
  260.    * @param callback_object call_obj the object to deliver callbacks to.
  261.    * @param int             width    the width of the label in pixels.
  262.    * @param Font            f        the font to use for this object (or pass 
  263.    *                                 null to get the default font).
  264.    * @param String          l        the label's string.
  265.    */
  266.   public label_toggle(boolean radio,callback_object call_obj, int width, 
  267.               Font f, String l) 
  268. {
  269.  
  270.     /* do the super initialization */
  271.     super(0,0,call_obj);
  272.  
  273.     is_radio_button=radio;
  274.     _requested_width=width;
  275.  
  276.     /* set the font */
  277.       _font=f;
  278.  
  279.     /* now fix the text and draw the images */
  280.     set_text(l);
  281.   }
  282.  
  283.    //had:
  284.    //* @exception bad_value PROPAGATED
  285.    //* @exception general PROPAGATED
  286.  
  287.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  288.  
  289.   /**
  290.    * Same as full constructor, but takes a pair of images to use for the 
  291.    * display of the interactor.
  292.    *
  293.    * @param int             x        the x position of the object.
  294.    * @param int             y        the y position of the object.
  295.    * @param boolean         radio    true if you want the look of a radio 
  296.    *                                 button, false if you want the look of a 
  297.    *                                 checkbox.
  298.    * @param callback_object call_obj the object to deliver callbacks to.
  299.    * @param int             width    the width of the label in pixels.
  300.    * @param Font            f        the font to use for this object (or pass 
  301.    *                                 null to get the default font).
  302.    * @param String          l        the label's string.
  303.    * @param loaded_image[] im the array of images to use for this interactor's
  304.    *                          display (array must be of size 2)
  305.    */
  306.   public label_toggle(int x, int y, boolean radio,
  307.               callback_object call_obj, int width, 
  308.               Font f, String l, loaded_image[] im)
  309.   {
  310.     /* do the super initialization */
  311.     super(x,y,call_obj);
  312.     _requested_width=width;
  313.     /* set which kind we are */
  314.     is_radio_button=radio;
  315.     /* set the font */
  316.       _font=f;
  317.     /* set the images */
  318.       _images = im;
  319.     /* now fix the text and draw the images */
  320.     set_text(l);
  321.   }
  322.  
  323.   /**
  324.    * Constructor which makes a label toggle from a pair of images and
  325.    * doesn't require x and y coordinates.
  326.    *
  327.    * @param callback_object call_obj the object to deliver callbacks to.
  328.    * @param int             width    the width of the label in pixels.
  329.    * @param Font            f        the font to use for this object (or pass 
  330.    *                                 null to get the default font).
  331.    * @param String          l        the label's string.
  332.    * @param loaded_image[] im the array of images to use for this interactor's
  333.    *                          display (array must be of size 2)
  334.    */
  335.   public label_toggle(boolean radio,callback_object call_obj, int width, 
  336.               Font f, String l, loaded_image[] im)
  337.   {
  338.     /* do the super initialization */
  339.     super(0,0,call_obj);
  340.     is_radio_button=radio;
  341.     _requested_width=width;
  342.     /* set the font */
  343.       _font=f;
  344.     /* set the images */
  345.       _images = im;
  346.     /* now fix the text and draw the images */
  347.     set_text(l);
  348.   }
  349.  
  350. }
  351. /*=========================== COPYRIGHT NOTICE ===========================
  352.  
  353. This file is part of the subArctic user interface toolkit.
  354.  
  355. Copyright (c) 1996 Scott Hudson and Ian Smith
  356. All rights reserved.
  357.  
  358. The subArctic system is freely available for most uses under the terms
  359. and conditions described in 
  360.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  361. and appearing in full in the lib/interactor.java source file.
  362.  
  363. The current release and additional information about this software can be 
  364. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  365.  
  366. ========================================================================*/
  367.